package com.lipaluma.xml.merge;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

import com.lipaluma.exceptions.CsvParserException;
import com.lipaluma.field.merge.FieldMergedAlgorithm;

@XmlType(name=XmlCustomMerge.FIELD_MERGED_CUSTOM)
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlCustomMerge implements XmlFieldMerged {
	public static final String FIELD_MERGED_CUSTOM = "field-merged-custom";
	
	@XmlAttribute(name="with", required=true)
	private Class<? extends FieldMergedAlgorithm> fieldMergedAlgorithm;

	@XmlElement(name="param")
	private String[] params;

	@Override
	public FieldMergedAlgorithm getAlgorithm() {
		try {
			return fieldMergedAlgorithm.newInstance();
		} catch (InstantiationException e) {
			throw new CsvParserException("The algorithm merge "+fieldMergedAlgorithm+" can not be instanciated", e);
		} catch (IllegalAccessException e) {
			throw new CsvParserException("The algorithm merge "+fieldMergedAlgorithm+" can not be accessed", e);
		}
	}

	@Override
	public String[] getParams() {
		if(this.params == null)
			return new String[0];
		return this.params;
	}
}
